package jt.classic.app.clgl.clsqd;

import jt.classic.system.ISystem;
import jt.classic.system.SimpleAction;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.user.IUser;

import org.limp.basework.ProcessorException;
import org.limp.basework.console.XSystemConsole;
import org.limp.basework.console.XSystemConsoleRunException;
import org.limp.basework.impl.CommonBean4HSQ;

import org.limp.mine.Controller;
import org.limp.mine.Label;
import org.limp.mine.QueryJack;
import org.limp.mine.QueryJackFactory;
import org.limp.mine.StringTool;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import java.sql.Connection;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 车辆申请单管理
 * 
 * @author zhuhj
 * 
 */
public class ClsqdAction extends SimpleAction {

	/** ***************车辆申请单***************************** */

	/**
	 * 列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Connection conn = null;

		CommonBean4HSQ req = new CommonBean4HSQ(request);

		Map param = req.getResource();

		String pageNO = StringTool.checkString(param.get("pageNO"));
		
        String type = StringTool.checkString(param.get("type"));//页面类型 select
        
		String v_cdh = StringTool.checkString(param.get("v_cdh"));
		String v_ycsjq = StringTool.checkString(param.get("v_ycsjq"));
		String v_ycsjz = StringTool.checkString(param.get("v_ycsjz"));
		request.setAttribute("v_cdh", v_cdh);
		request.setAttribute("v_ycsjq", v_ycsjq);
		request.setAttribute("v_ycsjz", v_ycsjz);

		String clsqd_zt = StringTool.checkString(param.get("clsqd_zt"));

		IUser user = ISystem.getSessionUser(request);

		StringBuffer where = new StringBuffer(" WHERE 1=1 ");

		if (ISystem.isAdmin(user)) {
			// donothing
		} else if (ISystem.matchrole(user, "clgly")) {
			// if (user.igroup() != null) {
			// String gid = user.igroup().ibranchgroup(0);
			//
			// where.append(" AND HYS_GSID_NAME = '" + gid + "' ");
			// }
		} else {
			// 普通用户只能查看自己提交的申请单
			where.append(" AND CLSQD_TJR = '" + user.id() + "' ");
		}
		// 转向
		String forward = "list";
		String url = "";
		url = request.getContextPath() + "/app/clsqd.do?method=list";
		forward = "list";
		

        if (type == "") { //列表页面
            url = request.getContextPath() + "/app/clsqd.do?method=list";
            forward = "list";
        } else if (type.equals("select")) { //选择页面
        	 url = request.getContextPath() + "/app/clsqd.do?method=list&type=select"; 
            forward = "list_select";
        }
		

		if (!v_cdh.equals("")) {
			where.append(" AND CLSQD_ID = " + v_cdh + " ");
		}
		if (!v_ycsjq.equals("")) {
			where.append(" AND CLSQD_YCSJQ >= '" + v_ycsjq + "' ");
		}
		if (!v_ycsjz.equals("")) {
			where.append(" AND CLSQD_YCSJZ <= '" + v_ycsjz + "' ");
		}

		String sql1 = "SELECT COUNT(1) FROM APP_CLSQD " + where.toString();
		String sql2 = "SELECT * FROM APP_CLSQD " + where.toString()
				+ " ORDER BY CLSQD_ID DESC";

		try {
			SystemDataSource ds = SystemDataSource.getInstance();
			conn = ds.getConnection();

			QueryJack plus = QueryJackFactory.getInstance().createQueryJack(
					false);

			// 设置
			plus.setParameter(QueryJackFactory.PAGINATION_SQL1, sql1);
			plus.setParameter(QueryJackFactory.PAGINATION_SQL2, sql2);
			plus.setParameter(QueryJackFactory.DATABASE_CONNECTION, conn);
			plus.setParameter(QueryJackFactory.PAGINATION_LINKSTRING, url);
			plus.setParameter(QueryJackFactory.PAGINATION_COUNT, "20");
			plus.setParameter(QueryJackFactory.PAGINATION_PAGENO, pageNO);

			// plus.setParameter(QueryJackFactory.PAGINATION_UPDATEPAGENO, "");
			List resultList = plus.work();

			// 获得跳转控制
			Controller controller = plus.getController("form1");

			// 设置调转类型
			controller.setType("input");

			// 显示标签
			Label label = plus.getLabel();

			// 设置显示标签类型
			label.setType("href");

			Object[] result = new Object[4];
			result[0] = resultList;
			result[1] = label;
			result[2] = controller;
			result[3] = pageNO;

			request.setAttribute("result", result);
		} catch (Exception ex) {
			ISystem.catchException(request, this, ex);
		} finally {
			SystemDataSource.closeConnection(conn);
		}

		return mapping.findForward(forward);
	}

	/**
	 * 新建车辆申请单
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward neu(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("neu");
	}

	/**
	 * 保存车辆申请单定义
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Connection conn = null;
		ActionForward forward = null;

		try {
			SystemDataSource ds = SystemDataSource.getInstance();
			conn = ds.getConnection();

			XSystemConsole console = new XSystemConsole();
			console.RUN2SAVE(request, conn);
			conn.commit();

			String url = "/clsqd.do?method=list";

			forward = new ActionForward();
			forward.setPath(url);
			forward.setRedirect(true);
		} catch (XSystemConsoleRunException ex) {
			conn.rollback();
			ISystem.catchException(request, this, ex);
		} finally {
			SystemDataSource.closeConnection(conn);
		}

		return forward;
	}

	/**
	 * 保存车辆申请单定义
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Connection conn = null;
		ActionForward forward = null;

		try {
			SystemDataSource ds = SystemDataSource.getInstance();
			conn = ds.getConnection();

			String clsqd_id = StringTool.checkString(request
					.getParameter("clsqd_id"));
			if (!clsqd_id.equals("")) {
				ClsqdManager manager = new ClsqdManager(conn);
				manager.delectCLZP(clsqd_id);
				manager.manualSaveX("APP_CLZP", "countO", request, conn,
						clsqd_id);
				// manager.clean();
			}

			XSystemConsole console = new XSystemConsole();
			console.RUN2REMIX(request, conn);

			conn.commit();

			String url = "/clsqd.do?method=list";

			forward = new ActionForward();
			forward.setPath(url);
			forward.setRedirect(true);
		} catch (XSystemConsoleRunException ex) {
			conn.rollback();
			ISystem.catchException(request, this, ex);
		} catch (ProcessorException ex) {
			conn.rollback();
			ISystem.catchException(request, this, ex);
		}

		finally {
			SystemDataSource.closeConnection(conn);
		}

		return forward;
	}

	/**
	 * 编辑车辆申请单
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String id = StringTool.checkString(request.getParameter("id"));
		ClsqdManager manager = new ClsqdManager();

		IClsqd clsqd = manager.getClsqdById(id);
		manager.clean();
		request.setAttribute("clsqd", clsqd);

		return mapping.findForward("edit");
	}
	
	/**
     * 按月统计查询
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward listyp_query(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;
        
        Date date= new Date();
        
        int year=date.getYear()+1900;
        
        CommonBean4HSQ req = new CommonBean4HSQ(request);

        Map param = req.getResource();

        String pageNO = StringTool.checkString(param.get("pageNO"));

        String STAMP_NO = StringTool.checkString(param.get("STAMP_NO"));

        request.setAttribute("STAMP_NO", STAMP_NO);
        
        String myyear = StringTool.checkString(param.get("myyear"));
        
        request.setAttribute("myyear", myyear);
        
        if(!"".equals(myyear)){
        	year = Integer.parseInt(myyear);
        }else{}
        
        String start_time = year+"0001000000";
        String end_time = year+"1231235959";
        
        /*String userid = StringTool.checkString(param.get("CK_USERID"));

        request.setAttribute("userid", userid);
*/
        IUser user = ISystem.getSessionUser(request);

        StringBuffer where = new StringBuffer(" WHERE CARSQ_DATE>='"+start_time+"' AND " +
        		"CARSQ_DATE<='"+end_time+"' AND CARSQ_STATUS=1" );

        /*if (ISystem.isAdmin(user)) {
            //donothing
        } else {
            if (user.igroup() != null) {
                String gname = user.igroup().ibranchgroup(0);
                String uid = user.id();
                where.append(" AND WJCS.USERID = '" + uid + "' ");
            }
        }*/

        if (!STAMP_NO.equals("")) {
            where.append(" AND STAMP_NO='"+STAMP_NO+"'");
        }

        where.append(" GROUP BY STAMP_NO");

        String sql1 = "SELECT COUNT(DISTINCT(STAMP_NO)) as num FROM APP_CARSQ " +
            where.toString();
        String sql2 = "SELECT SUM(STAMP_FP) as STAMP_FP,STAMP_NO,STAMP_COUNT FROM APP_CARSQ " + where.toString();
            //" ORDER BY  WJJS.WJTIME ASC";

        String url = request.getContextPath() + "/app/clsqd.do?method=listyp_query";

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            QueryJack plus = QueryJackFactory.getInstance().createQueryJack(false);

            // ����
            plus.setParameter(QueryJackFactory.PAGINATION_SQL1, sql1);
            plus.setParameter(QueryJackFactory.PAGINATION_SQL2, sql2);
            plus.setParameter(QueryJackFactory.DATABASE_CONNECTION, conn);
            plus.setParameter(QueryJackFactory.PAGINATION_LINKSTRING, url);
            plus.setParameter(QueryJackFactory.PAGINATION_COUNT, "20");
            plus.setParameter(QueryJackFactory.PAGINATION_PAGENO, pageNO);

            // plus.setParameter(QueryJackFactory.PAGINATION_UPDATEPAGENO, "");
            List resultList = plus.work();

            /**
            if (!resultList.isEmpty()) {
                ModuleManager manager = new ModuleManager(conn);

                for (int i = 0; i < resultList.size(); i++) {
                    String id = (String) ((Map) resultList.get(i)).get("ID");
                    IModule module = manager.getModuleById(id);
                    ((Map) resultList.get(i)).put("module", module);
                }
            }
                        */

            // �����ת����
            Controller controller = plus.getController("form1");

            // ���õ�ת����
            controller.setType("input");

            // ��ʾ��ǩ
            Label label = plus.getLabel();

            // ������ʾ��ǩ����
            label.setType("href");

            Object[] result = new Object[4];
            result[0] = resultList;
            result[1] = label;
            result[2] = controller;
            result[3] = pageNO;
            
            request.setAttribute("result", result);
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("listyp_query");
    }
}
